function ListNode(val, next) {
    this.val = (val === undefined ? 0: val)
    this.next = (next === undefined ? null : next)
}
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
// 迭代的方式
var mergeTwoLists = function(l1, l2) {
    //设定一个呀节点
    let newHead = new ListNode(0)
    let pre = newHead
    while (l1 != null && l2 != null )  {
        if (l2.val > l1.val) {
            pre.next = l1;
            l1 = l1.next
        } else {
            pre.next = l2;
            l2 = l2.next
        }
        pre = pre.next
    }
    if (l1 == null) {
        pre.next = l2
    }else {
        pre.next = l1
    }
    return newHead.next
};

// 递归的方式
var mergeTwoLists = function(l1, l2) {
    if (l1 == null) {
        return l2
    } else if (l2 == null) {
        return l1
    }else if (l1.val < l2.val) {
        l1.next = mergeTwoLists(l1.next, l2)
        return l1
    } else {
        l2.next = mergeTwoLists(l1, l2.next)
        return l2
    }
}
let l1 = new ListNode(1)
let p1 = new ListNode(2)
let p2 = new ListNode(4)
l1.next = p1
p1.next = p2

let l2 = new ListNode(1)
let p3 = new ListNode(3)
let p4 = new ListNode(4)
l2.next = p3
p3.next = p4

console.log(mergeTwoLists(l1,l2))